#!/bin/bash

# Determine the parent directory of the script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

# Default values
IN_DIR="$PROJECT_DIR"
OUT_DIR=""

# Parse arguments
while getopts "i:o:" opt; do
  case $opt in
    i) IN_DIR="$OPTARG";;
    o) OUT_DIR="$OPTARG";;
    *) echo "Usage: $0 -i <input_directory> -o <output_directory>" >&2
       exit 1;;
  esac
done

# Check if OUT_DIR is specified
if [ -z "$OUT_DIR" ]; then
  echo "Error: Output directory (-o) is not specified." >&2
  echo "Usage: $0 -i <input_directory> -o <output_directory>" >&2
  exit 1
fi

# Create the output directory if it doesn't exist
mkdir -p "$OUT_DIR"

# Create subdirectories inside the output directory
mkdir -p "$OUT_DIR/Input"
mkdir -p "$OUT_DIR/Output"
mkdir -p "$OUT_DIR/Video"
mkdir -p "$OUT_DIR/Doc"
mkdir -p "$OUT_DIR/Tool"

# Copy all contents from the input directory to the Tool directory, excluding .git and untracked files
git -C "$IN_DIR" ls-files | rsync -av --files-from=- --exclude='.git' --exclude='.gitignore' "$IN_DIR/" "$OUT_DIR/Tool"
# Copy specific files
cp "$IN_DIR/scripts/vivo_docker_build_and_attach.sh" "$OUT_DIR/enter_docker_env.sh"
cp "$IN_DIR/scripts/vivo_run_inside_container.sh" "$OUT_DIR/run.sh"
cp "$IN_DIR/docker/Dockerfile" "$OUT_DIR/Dockerfile"

# Print helpful messages
echo "Directories and files have been successfully set up."
echo "Input directory: $IN_DIR"
echo "Output directory: $OUT_DIR"
echo "Contents from $IN_DIR have been copied to $OUT_DIR/Tool, excluding .git, .gitignore, and untracked files."
echo "Copied $IN_DIR/script/s1.sh to $OUT_DIR/run.sh"
echo "Copied $IN_DIR/docker/Dockerfile to $OUT_DIR/Dockerfile"
echo "You can now proceed with your project setup."